package sma.fbss.gui;

//A program to demonstrate the use of JTextFields's

//Import Statements
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import sma.fbss.agent.Buyer;
import sma.fbss.model.MDFBaller;
import sma.fbss.model.MDPurchase;
import sma.fbss.model.MDSeller;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.util.*;

public class GUIBuyer extends JFrame implements ListSelectionListener
{
 /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	//Class Declarations
	JTabbedPane tabs = new JTabbedPane();
	JPanel jpnl_tabpage1 = new JPanel();
	JPanel jpnl_tabpage2 = new JPanel();
 JButton jbt_display;
 JButton jbt_choisir;
 JScrollPane jsp_display;
 JLabel jlb_club;
 JComboBox jcbb_club;
 
 private JList jl_fb_list;
 private JSplitPane split_pane;
 
 private MDFBaller[] fb_list;
 private MDSeller[] seller_list;
 private Vector<String> club_texts;
 
 private JLabel jlb_name_fb;
 private JLabel jlb_height_fb;
 private JLabel jlb_birthday_fb;
 private JLabel jlb_price_fb;
 private JTextField jtf_name_fb;
 private JTextField jtf_height_fb;
 private JTextField jtf_birthday_fb;
 private JTextField jtf_price_fb;
 String disp = "";
 TextHandler handler  = null;
 
 Buyer agent;

 //Constructor
 public GUIBuyer()
 {
	 initGUI();
	 setClubList();
	 this.setTitle("NGUYEN Quoc Khai - Supportaire");
 }
 
	public GUIBuyer(Buyer _agent) {
		super();
		this.agent = _agent;
		if (agent != null)
			this.setTitle(this.agent.data_model.name_club + " - "
					+ this.agent.data_model.id_club);
		initGUI();
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				setClubList();
				GUIBuyer.this.setLocationRelativeTo(null);
			}
		});
	}

public void initGUI()
{
	GridBagLayout grid = new GridBagLayout();
	jpnl_tabpage1.setLayout(grid);
	tabs.addTab("Tab page 1", null, jpnl_tabpage1, null);
	tabs.addTab("Tab page 2", null, jpnl_tabpage2, null);
	this.getContentPane().add(tabs);
	
	jbt_display = new JButton("Listez les footballeurs concernant");
	jbt_choisir = new JButton("Demandez l'équipe propriétaire");
	
	
	jlb_name_fb = new JLabel("Nom et prenom");
	jlb_height_fb = new JLabel("La taille");
	jlb_birthday_fb = new JLabel("Date de naissance");
	jlb_price_fb = new JLabel("Le prix");
	
	jtf_name_fb = new JTextField("Nom et prenom du footballeur");
	jtf_height_fb = new JTextField("La taille du footballeur");
	jtf_birthday_fb = new JTextField("Date de naissance du footballeur");
	jtf_price_fb = new JTextField("Le prix du footballeur");
	
	jlb_club = new JLabel("Quelle equipe voulez-vous choisir ?");
	
	jlb_club.setIcon(new ImageIcon(getClass().getClassLoader().getResource("one.png")));
	jlb_club.setPreferredSize(new java.awt.Dimension(382, 45));		
	jlb_club.setFont(new java.awt.Font("", 1, 13));
	jlb_club.setBackground(new java.awt.Color(255,255, 255));
	
	jcbb_club = new JComboBox();
	
	//jcbb_club.setModel(cbbm_club);
	
    jl_fb_list = new JList();
    jl_fb_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    jl_fb_list.setSelectedIndex(0);
    jl_fb_list.addListSelectionListener(this);
    
   
    JScrollPane listScrollPane = new JScrollPane(jl_fb_list); 
    
    
    
    JPanel info_pane = new JPanel();
    info_pane.setLayout(new GridBagLayout());
    
    //Create a split pane with the two scroll panes in it.
    split_pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
                               listScrollPane, info_pane);
    split_pane.setOneTouchExpandable(true);

    //Provide minimum sizes for the two components in the split pane.
    Dimension minimumSize = new Dimension(100, 50);
    listScrollPane.setMinimumSize(minimumSize);
    info_pane.setMinimumSize(minimumSize);

    //Provide a preferred size for the split pane.
    split_pane.setPreferredSize(new Dimension(400, 200));

		/*add all initialized components to the container*/
    addComponent(jpnl_tabpage1, jlb_club, 0, 0, 4, 1, GridBagConstraints.WEST, GridBagConstraints.BOTH);
    addComponent(jpnl_tabpage1, jcbb_club, 0, 1, 4, 1, GridBagConstraints.WEST, GridBagConstraints.BOTH);
    addComponent(jpnl_tabpage1, jbt_display, 0, 2, 1, 1, GridBagConstraints.WEST, GridBagConstraints.BOTH);
    addComponent(jpnl_tabpage1, jbt_choisir, 1, 2, 1, 1, GridBagConstraints.WEST, GridBagConstraints.BOTH);
    addComponent(jpnl_tabpage1, split_pane, 0, 3, 10, 6, GridBagConstraints.WEST, GridBagConstraints.BOTH);
    
    addComponent(info_pane, jlb_name_fb, 0, 0, 1, 1, GridBagConstraints.WEST, GridBagConstraints.BOTH);
	addComponent(info_pane, jtf_name_fb, 0, 1, 2, 1, GridBagConstraints.WEST, GridBagConstraints.BOTH);
	addComponent(info_pane, jlb_height_fb, 0, 2, 1, 1, GridBagConstraints.WEST, GridBagConstraints.BOTH);
	addComponent(info_pane, jtf_height_fb, 0, 3, 1, 1, GridBagConstraints.WEST, GridBagConstraints.BOTH);
	addComponent(info_pane, jlb_birthday_fb, 0, 4, 1, 1, GridBagConstraints.WEST, GridBagConstraints.BOTH);
	addComponent(info_pane, jtf_birthday_fb, 0, 5, 1, 1, GridBagConstraints.WEST, GridBagConstraints.BOTH);
	addComponent(info_pane, jlb_price_fb, 0, 6, 1, 1, GridBagConstraints.WEST, GridBagConstraints.BOTH);
	addComponent(info_pane, jtf_price_fb, 0, 7, 1, 1, GridBagConstraints.WEST, GridBagConstraints.BOTH);
	
	handler = new TextHandler();
		
	jbt_choisir.addActionListener(handler);
	jbt_display.addActionListener(handler);
	jcbb_club.addActionListener(handler);
	
		
	setSize(700, 400);
	setVisible(true);
}
	private static final Insets insets = new Insets(0, 0, 0, 0);
  @SuppressWarnings("unused")
  private static void addComponent(Container container, Component component, int gridx, int gridy,
	      int gridwidth, int gridheight, int anchor, int fill) {
	    GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0, 1.0,
	        anchor, fill, insets, 0, 0);
	    container.add(component, gbc);
}
  
	public void receiveMsg(String text) 
	{
		JOptionPane.showMessageDialog(this, text);
	}
  
  
	public void displayInfoFB(int id)
	{
		//Id inacceptable
		if(id < 0)
			return;
		//Acceptable
		{
			String bday = Long.toString(fb_list[id].birthday);
			String name = fb_list[id].name_footballer;
			String h = Float.toString(fb_list[id].height);
			String price = Float.toString(fb_list[id].price);
			
			String day = bday.substring(0, 2);
			String m = bday.substring(2, 4);
			String y = bday.substring(5);
			this.jtf_name_fb.setText(name);
			this.jtf_birthday_fb.setText(day + " - " + m + " - 1" + y);
			this.jtf_height_fb.setText(h);
			this.jtf_price_fb.setText(price);
		}
	}
	
	public void displayFBallerwithClub(int id)
	{
		String id_seller = "";
		if(id > -1)
			id_seller = seller_list[id].id_club;
		try {
			fb_list = new MDFBaller().getFBallerwithClub(id_seller);
			int fb_num = fb_list.length;
			String[] name = new String[10000];
			DefaultListModel model = new DefaultListModel();
			for(int i = 0; i < fb_num; i++)
			{
				if(fb_list[i] != null)
				{
					name[i] = fb_list[i].name_footballer;
					model.addElement(name[i]);
				}
			}
			
			jl_fb_list.setModel(model);
			
		} catch (Exception e) {
			// TODO Bloc catch généré automatiquement
			e.printStackTrace();
		}
	}
	
    //Listens to the list
    public void valueChanged(ListSelectionEvent e) {
        JList list = (JList)e.getSource();
        displayInfoFB(list.getSelectedIndex());
        //updateLabel(imageNames[list.getSelectedIndex()]);
        
    }

    //Used by SplitPaneDemo2
    public JList getImageList() {
        return jl_fb_list;
    }
    
    public void setClubList()
    {
    	try {
    		
			seller_list = new MDSeller().getAll();
			
			club_texts = new Vector<String>();
	    	club_texts.addElement("N'importe quelle equipe");
	    	int club_num = seller_list.length;
	    	for(int i = 0; i < club_num; i++)
	    	{
	    		if(seller_list[i] != null)
	    		{
	    			this.club_texts.addElement(seller_list[i].name_club +
	    					" - " + seller_list[i].id_club);
	    		}
	    	}
	    	ComboBoxModel model = new DefaultComboBoxModel(club_texts);
	    	jcbb_club.setModel(model);
			
		} catch (Exception e) {
			// TODO Bloc catch généré automatiquement
			e.printStackTrace();
		}
    	
    }

    public JSplitPane getSplitPane() {
        return split_pane;
    }
    
    public void choisirFB()
    {
    	String ach = this.agent.data_model.id_club;
    	String ven = fb_list[jl_fb_list.getSelectedIndex()].club;
    	String foot = fb_list[jl_fb_list.getSelectedIndex()].id_footballer;
    	String price = jtf_price_fb.getText();
    	String msg = ach + "//" + ven + "//" + foot + "//" + price;
    	agent.setMessage(msg, 0);
    	JOptionPane.showMessageDialog(this, agent.msg);
    	
    }
	
 //Inner Class TextHandler
 private class TextHandler implements ActionListener
 {
	public void actionPerformed(ActionEvent e)
	{
		int	id = jcbb_club.getSelectedIndex() - 1;
	    if(e.getSource() == jcbb_club)
	    {
	    	displayFBallerwithClub(id);
	    }
	    if(e.getSource() == jbt_choisir)
	    	choisirFB();
	}
 }
 
 /*
 public static void main(String argv[]) throws IOException 
 {
	 GUIBuyer gui = new GUIBuyer();
	 
	 
	 gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }*/
		
}

